/**=========================================================
 * Controller: SystemTaskController
  =========================================================*/
App.controller('SystemTaskController', ['$scope', 'toaster', 'ngDialog', 'Utils', 'HttpUtils', 'DialogUtils', 'SimpleUiGrid',
    function($scope, toaster, ngDialog, utils, httpUtils, dialogUtils, simpleUiGridService) {
        //===========================taskGrid=================================            
        var taskGridColumns = [
            {
                displayName: '任务组',
                field: 'job_group'
            },
            {
                displayName: '任务名称',
                field: 'job_name'
            },
            {
                displayName: '处理类',
                field: 'class_name'
            },
            {
                displayName: '定时配置',
                field: 'cron',
                width: '150'
            },
            {
                displayName: '参数1',
                field: 'param1',
                width: '150'
            },
            {
                displayName: '参数2',
                field: 'param2',
                width: '150'
            },
            {
                displayName: '状态',
                field: 'jobStatus',
                width: '120'
            },
            { 
                name: '', 
                field: 'widgets', 
                width: '150',
                enableCellEdit: false,
                cellTemplate: '\
                    <div class="ui-grid-cell-contents" style="text-align:center;">\
                        <button ng-click="grid.appScope.stopTask(row.entity)" title="暂停任务" class="btn btn-xs btn-success">\
                            <em class="fa fa-pause"></em>\
                        </button>\
                        <button ng-click="grid.appScope.continueTask(row.entity)" title="继续任务" class="btn btn-xs btn-warning">\
                            <em class="fa fa-play"></em>\
                        </button>\
                        <button ng-click="grid.appScope.deleteTask(row.entity)" title="删除任务" class="btn btn-xs btn-info">\
                            <em class="fa fa-close"></em>\
                        </button>\
                        <button ng-click="grid.appScope.runJob(row.entity)" title="立即执行" class="btn btn-xs btn-danger">\
                            <em class="fa fa-arrow-circle-right"></em>\
                        </button>\
                    </div>\
                '
            }
        ];
        
        $scope.queryParams = {};
        $scope.searchTaskGrid = function() {
            taskGrid.draw($scope.queryParams);
        }; 

        var taskGrid = simpleUiGridService.init($scope, 
            {
                optionModel: 'taskGridOptions',
                pageOptionModel: 'taskGridPageOptions',
                gridApi: 'taskGridApi',
                url: '/task/getAllTasks',
                needExport: false,
                needPage: false,
                columns: taskGridColumns
            }
        );
        
        $scope.stopTask = function(data){
            httpUtils.post('/task/stopTask',data,
                function(result){
                    if (result.success){
                        $scope.searchTaskGrid();
                        toaster.pop('success', '', '操作成功');
                    }else{
                        toaster.pop('error', '', result.msg);
                    }
                },
                function(){
                    toaster.pop('error', '', '操作失败');
                }
            );
        };
        
        $scope.continueTask = function(data){
            httpUtils.post('/task/continueTask',data,
                function(result){
                    if (result.success){
                        $scope.searchTaskGrid();
                        toaster.pop('success', '', '操作成功');
                    }else{
                        toaster.pop('error', '', result.msg);
                    }
                },
                function(){
                    toaster.pop('error', '', '操作失败');
                }
            );
        };
        
        $scope.deleteTask = function(data){
            dialogUtils.confirm('确定要删除吗?',
                function () {
                    httpUtils.post('/task/deleteTask',data,
                        function(result){
                            if (result.success){
                                taskGrid.deleteRow(data);
                                toaster.pop('success', '', '删除成功');
                            }else{
                                toaster.pop('error', '', result.msg);
                            }
                        },
                        function(){
                            toaster.pop('error', '', '删除失败');
                        }
                    );
                }
            ); 
        };
        
        $scope.runJob = function(data){
            dialogUtils.confirm('确定要立即执行该任务吗?',
                function () {
                    httpUtils.post('/task/runTask',data,
                        function(result){
                            if (result.success){
                                toaster.pop('success', '', '执行成功');
                            }else{
                                toaster.pop('error', '', result.msg);
                            }
                        },
                        function(result){
                            toaster.pop('error', '', '执行失败');
                        }
                    );
                }
            );       
        };

        
//===========================init================================= 
        $scope.searchTaskGrid();
    }
]);
